<template>
	<view>
		<cu-custom bgColor="bg-red" :isBack="true">		
		<block slot="backText"></block>
			<block slot="content">{{title}}</block>
		</cu-custom>
		<view class="bg-white" > <view style="margin-left: 20px;"v-if="member && member.is_auta==0 && member.current_level<4">激活状态：待激活</view><view style="margin-left: 20px;"v-else>激活状态：已激活</view></view>
		<scroll-view scroll-x class="bg-white nav text-center my-scroll-view" style="margin-top: 36rpx;">
			<!-- #ifdef APP-VUE -->
			<view style="margin-top:25px ;"></view>
			<!-- #endif -->
			<view class="cu-item" v-for="(item,index) in listObj" :key="index" :class="index==TabCur?'cur text-'+theme:'text-gray'"
			 @tap="tabSelect" :data-id="index">
				{{item.title}}
			</view>
		</scroll-view>
		<view v-for="(item,index) in listObj" :key="index" v-if="index==TabCur" class="list-content">
			<!-- 我的奖品 -->
			<block v-for="(reward ,i) in item.list" :key="i" v-if="TabCur==0">
				<view class="reward-box">
					
					<view class="goods">
						<view>
							<checkbox class="round"  :checked="true" :value="reward.id"
							 @tap="selectChange(i)"></checkbox>

						</view>
						<view class="thumb">
							<image :src="reward.member.head_portrait" mode="aspectFit"></image>
						</view>
						<view class="info">
							<view class="title">{{reward.member.username}} <text style="color: #0081FF;margin-left: 200rpx;">{{reward.level.name}}</text></view>
							<view class="time">用户id：{{reward.member.id}}</view>
							<view class="time">手机号码：{{reward.member.moblie}}</view>
							<view class="time">微信号：{{reward.wx_member}}</view>
						</view>
					</view>
					<view class="action-box" >
						<view class="time">申请时间：{{$common.timeFormat(reward.updated_at)}}</view>
						<text class="goods-detail"   @click="r_reward()">驳回</text>
						<text class="goods-detail"   @click="r_pass()">通过</text>
					</view>
				</view>
				
			</block>
			<!-- <view class="cu-bar bg-white tabbar border shop foot" v-if="TabCur==0">
				<view class="action" @tap="selectAllChange">
					<checkbox class='round' :class="[selectAll?'checked':'',theme]" :checked="selectAll?true:false" value="1">全选</checkbox>
				</view>
				
				<button class="cu-btn round shadow-blur margin-right-sm" :class="'bg-'+theme" @tap="goOrder">去结算</button>
			</view> -->
			<!-- 参与记录 -->
			<!-- #ifdef APP-VUE -->
			<view style="margin-top:-10px ;"></view>
			<!-- #endif -->
			<view class="join-box" v-if="TabCur==1">
				<block v-for="(reward ,i) in item.list" :key="i" >
					<view class="reward-box">
						<view class="goods">
							<view class="thumb">
								<image :src="reward.member.head_portrait" mode="aspectFit"></image>
							</view>
							<view class="info">
								<view class="title">{{reward.goods.name}}</view>
								<view class="time">领取时间：{{$common.timeFormat(reward.updated_at)}}</view>
							</view>
						</view>
						<view class="action-box" >
							<view class="time">申请时间：{{$common.timeFormat(reward.updated_at)}}</view>
							<text class="goods-detail"  v-if="reward.status == 1" @click="receiveReward(reward.good_id,reward.id)">查看订单</text>
						</view>
						
						
					</view>
				</block>
			</view>
			<view class="cu-load margin-center" :class="item.loading?'loading':'over'"></view>
		</view>

		<serviceView ref="serviceView"></serviceView>
	</view>
</template>

<script>
	import serviceView from '@/components/default/serviceView.vue'
	export default {
		components: {
			serviceView,
		},
		filters: {},
		data() {
			return {
				theme: this.$config.THEME(),
				TabCur: 0,
				member:null,
				selectAll:false,
				formData: {
					merchant_id: 0,
					is_channel: 0,
					address_id: 0,
					buyer_message: '',
					data: {
						sku_id:0,
						num:1
					},
					olddata: {
						sku_id:0,
						num:1
					},
					shipping_type: 1,
					type: 'buy_now',
					point: 0,
					lottlog_id:0
				},
				title: '审批管理',
				listObj: [{
						title: '待审批',
						list: [],
						hasMore: true,
						loading: false,
						page: 1,
					},
					{
						title: '已审批',
						list: [
							
						],
						hasMore: true,
						loading: false,
						page: 1,
					}
				],
			}
		},
		onLoad(options) {
			this.listObj[this.TabCur].list = []
			this.listObj[this.TabCur].hasMore = true
			this.listObj[this.TabCur].page = 1
			this.listObj[this.TabCur].loading = false
			this.getList(this.TabCur)
		},
		onShow() {
			this.$common.getMember().then((member) => {
				this.member = member
				console.log(this.member);
			})
			// this.$nextTick(function() {
			// 	uni.startPullDownRefresh({})
			// })
		},
		methods: {
			// selectAllChange() {
			// 	this.selectAll = !this.selectAll
			
			// 	let listObj = this.listObj[0]
			// 	for (let i in listObj) {
			// 		if (listObj[i].product.product_status == 1) {
			// 			listObj[i].selected = this.selectAll
			// 		} else listObj[i].selected = false
			// 	}
			// 	this.listObj[0] = listObj
				
			// },
			selectChange(i) {
				let listObj = this.listObj[0]
				listObj.list[i].selected = !listObj.list[i].selected
				this.listObj[0] = listObj
				console.log(listObj)
				// this.$forceUpdate()
				// this.selectAll = false
				// this.mathTotalPrice()
			},
			tabSelect(e) {
				if (this.TabCur != e.currentTarget.dataset.id) {
					this.TabCur = e.currentTarget.dataset.id
					this.listObj[this.TabCur].list = []
					this.listObj[this.TabCur].hasMore = true
					this.listObj[this.TabCur].page = 1
					this.listObj[this.TabCur].loading = false
					this.getList(this.TabCur)
				}
			},
			kf_show(url){
				this.$refs.serviceView.img_url = url
				this.$refs.serviceView.show()
			},
			receiveReward(goods_id, id){
				if(this.member.current_level< 4 && this.member.is_auta==0){
					uni.showModal({
						title: '请先激活再领取',
						content: '只需激活一次，后续无需再次激活',
						success: function (res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/member/appy_comshare?id='+id				
								})
							} else if (res.cancel) {
								return;
							}
						}
					});
					return;
				}
				if (goods_id > 0 && id > 0) {
					uni.showLoading({
						title: '正在处理...'
					})
					this.$http
						.post('points_receive_reward', {
							gettype:'address_id',
							id:id
						})
						.then((response) => {
							if (response.data.data.status != '-1' && response.data.data.address_id > 0) {
								this.formData.olddata.sku_id = goods_id
								this.formData.olddata.num = 1
								this.formData.data = JSON.stringify(this.formData.olddata)
								this.formData.address_id = response.data.data.address_id
								this.formData.points_lottlog_id = id
								uni.showLoading({
									title: '正在处理...'
								})
								this.$http
									.post('order_create', this.formData)
									.then((response) => {
										uni.hideLoading()
										var order_id = response.data.data.id
										if (order_id > 0) {
											var that = this
											this.$common.showSuccess('领取成功', function() {
												that.listObj[that.TabCur].list = []
												that.listObj[that.TabCur].hasMore = true
												that.listObj[that.TabCur].page = 1
												that.listObj[that.TabCur].loading = false
												that.getList(that.TabCur)
											})
										} else {
											that.listObj[that.TabCur].list = []
											that.listObj[that.TabCur].hasMore = true
											that.listObj[that.TabCur].page = 1
											that.listObj[that.TabCur].loading = false
											that.getList(that.TabCur)
										}
										
									})
									.catch((response) => {
										console.log(response)
										uni.hideLoading()
									})
							} else {
								if (response.data.data.address_id == 0) {
									if (response.data.data.status == 400) {
										this.$common.showError("已领取过奖励")
										return
									} else {
										this.$common.showError("请先添加收货地址")
										setTimeout(function(){
											uni.navigateTo({
												url:'/pages/member/address/list'
											})
										}, 750)
										
									}
								}
								uni.hideLoading()
							}
							
							
						})
						.catch((response) => {
							console.log(response)
							uni.hideLoading()
						})
					
				} else {
					this.$common.showError("奖品参数错误")
					return
				}
			},
			getList(index) {
				let obj = this.listObj[index]
				if (!obj.hasMore || obj.loading) return

				this.listObj[index].loading = true
				
				if(index>0){ //参与记录
					let params = {
						page: obj.page,
						type:1
					}
					this.$http
						.get('pass_list', params)
						.then((response) => {
							console.log(response)
							let list = obj.list
							if (response.data.data.length > 0) {
								list = list.concat(response.data.data)
							} else {
								this.listObj[index].hasMore = false
							}
							this.listObj[index].list = list
							this.listObj[index].loading = false
							this.listObj[index].page++
							uni.stopPullDownRefresh()
						})
						.catch((response) => {
							console.log(response)
							uni.stopPullDownRefresh()
						})
				}else{ //获奖记录
					let params = {
						page: obj.page,
						type:2
					}
					this.$http
						.get('pass_list', params)
						.then((response) => {
							console.log(response)
							let list = obj.list
							if (response.data.data.length > 0) {
								list = list.concat(response.data.data)
							} else {
								this.listObj[index].hasMore = false
							}
							this.listObj[index].list = list
							this.listObj[index].loading = false
							this.listObj[index].page++
							uni.stopPullDownRefresh()
						})
						.catch((response) => {
							console.log(response)
							uni.stopPullDownRefresh()
						})
				}
			},
		},
		onPullDownRefresh() {
			this.listObj[this.TabCur].list = []
			this.listObj[this.TabCur].hasMore = true
			this.listObj[this.TabCur].page = 1
			this.listObj[this.TabCur].loading = false
			this.getList(this.TabCur)
		},
		onReachBottom() {
			this.getList(this.TabCur)
		},
	}
</script>

<style>
	.my-scroll-view{
		position: fixed;
		top: 45px;
		left: 0;
		border-bottom: 1rpx solid #EAEAEA;
	}
	.list-content{
		padding-top: 45px;
	}
	.reward-box{
		margin: 20rpx;
		padding: 30rpx 20rpx;
		background-color: #FFFFFF;
		border-radius: 12rpx;
	}
	.reward-box .goods{
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
	.goods .thumb{
		margin-right: 20rpx;
		flex-shrink: 0;
		padding: 10rpx;
		width: 148rpx;
		height: 140rpx;
		background-color: #F2F2F2;
		border-radius: 8rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}
	.goods .thumb image{
		max-width: 138rpx;
		max-height: 130rpx;
	}
	.goods .info{
		flex: 1;
		/* height: 140rpx; */
		display: flex;
		flex-direction: column;
		align-items: space-between;
	}
	.info .title{
		flex: 1;
		font-size: 28rpx;
		font-weight: bold;
		color: #333333;
		display: -webkit-box;    
		-webkit-box-orient: vertical;    
		-webkit-line-clamp: 2;    
		overflow: hidden;
		line-height: 40rpx;
	}
	.info .time{
		margin-top: 10rpx;
		font-size: 24rpx;
		color: #999999;
	}
	.reward-box .action-box{
		margin-top: 40rpx;
		padding-top: 24rpx;
		border-top: 1rpx solid #EAEAEA;
		display: flex;
		justify-content: flex-end;
		align-items: center;
	}
	.action-box text{
		width: 100rpx;
		text-align: center;
		height: 54rpx;
		line-height: 54rpx;
		border: 1rpx solid #3282FF;
		border-radius: 17rpx;
		font-size: 18rpx;
	}
	.action-box text.goods-detail{
		margin-left: 14rpx;
		color: #3282FF;
		border: 1rpx solid #3282FF;
	}
	.action-box text.order-detail{
		color: #FFFFFF;
		background-color: #3282FF;
	}
</style>

<style>
	.join-box{
		padding: 0 20rpx;
		background-color: #FFFFFF;
	}
	.join-box .stand-line, .top-line{
		display: flex;
		justify-content: space-between;
		text-align: center;
		padding: 24rpx 0;
		border-bottom: 1rpx solid #E2E1E1;
	}
	.top-line text{
		flex: 33.33% 0 0;
		flex-shrink: 0;
		font-size: 28rpx;
		font-weight: bold;
		color: #333333;
		text-align: center;
	}	
	.stand-line text{
		flex: 33.33% 0 0;
		flex-shrink: 0;
		font-size: 28rpx;
		color: #666666;
		text-align: center;
	}
	.stand-line text.isreward{
		color: #FF1313;
	}
</style>